/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddTransactionRelease.java
 *
 * Created on Nov 6, 2011, 1:49:37 AM
 */
package mpinventory;

import java.sql.*;
import com.michaelbaranov.microba.calendar.DatePicker;
import java.util.Calendar;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.ArrayList;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JFrame;
import javax.swing.JDialog;
import javax.swing.JOptionPane;
import javax.swing.table.DefaultTableModel;
import javax.swing.DefaultListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;
/**
 *
 * @author msi
 */
public class AddTransactionRelease extends javax.swing.JPanel {

    private Connection conn;
    private Dimension size;
    private DateFormat dateFormat=new SimpleDateFormat("MMMM dd, yyyy");
    private Calendar calendar=Calendar.getInstance();
    private Date date=new Date(calendar.get(calendar.YEAR)-1900,calendar.get(calendar.MONTH),calendar.get(calendar.DAY_OF_MONTH));
    private DatePicker datePicker=new DatePicker(date,dateFormat);
    private TransactionReleaseQuery releaseQuery;
    private InstallationQuery installationQuery;
    private List<Installation> installations; //list of all installations
    private List<Customer> customers; //list of all customers
    private List<String> serialNumbers;
    private List<Integer> installationIDs;
    private List<String> brands;
    private List<String> models;
    private List<Integer> quantities;
    private List<Float> prices; //list of aircons in table
    private DefaultTableModel model;
    public int width;
    public int height;
    public int screenWidth;
    public int screenHeight;
    private int nRows=0;
    public boolean backToMainMenu=false;
    private String data[][] = {};
    private String col[] = {"Serial Number", "Brand", "Model", "Quantity", "Price"};
    /** Creates new form AddTransactionRelease */
    public AddTransactionRelease() {
        initComponents();
    }
    public AddTransactionRelease(Connection conn, int screenWidth, int screenHeight) {
        this.conn=conn;
        this.screenWidth=screenWidth;
        this.screenHeight=screenHeight;
        myInitComponents();
        size=this.getPreferredSize();
        width=size.width+35;
        height=size.height+35;
        releaseQuery=new TransactionReleaseQuery(conn);
        model = new DefaultTableModel(data, col){
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                true, false, false, true, true
            };
            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        };
        tableTRACustomerOrders.setModel(model);
        tableTRACustomerOrders.getColumnModel().getColumn(0).setResizable(false);
        tableTRACustomerOrders.getColumnModel().getColumn(0).setPreferredWidth(120);
        tableTRACustomerOrders.getColumnModel().getColumn(1).setResizable(false);
        tableTRACustomerOrders.getColumnModel().getColumn(1).setPreferredWidth(100);
        tableTRACustomerOrders.getColumnModel().getColumn(2).setResizable(false);
        tableTRACustomerOrders.getColumnModel().getColumn(2).setPreferredWidth(95);
        tableTRACustomerOrders.getColumnModel().getColumn(3).setResizable(false);
        tableTRACustomerOrders.getColumnModel().getColumn(3).setPreferredWidth(10);
        tableTRACustomerOrders.getColumnModel().getColumn(3).setResizable(false);
        tableTRACustomerOrders.getColumnModel().getColumn(3).setPreferredWidth(50);
        initializeInstallationList();
        initializeCustomerList();
        installationIDs=new ArrayList<Integer>();
        txtTRAOrderID.setText(releaseQuery.setID()+"");
    }
    @Override
    public int getWidth(){
        return width;
    }
    @Override
    public int getHeight(){
        return height;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtTRAOrderID = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cbxTRAStatus = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        cbxTRABrand = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listTRAUnits = new javax.swing.JList();
        jLabel10 = new javax.swing.JLabel();
        btnTRAAdd = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        btnTRARemove = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableTRACustomerOrders = new javax.swing.JTable();
        btnTRASave = new javax.swing.JButton();
        btnTRACancel = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        listTRACustomers = new javax.swing.JList();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 25));
        jLabel1.setText("Add Customer Transaction");
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(376, 18, -1, -1));

        jLabel2.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel2.setText("Order ID:");
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 72, -1, -1));

        txtTRAOrderID.setEditable(false);
        txtTRAOrderID.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        add(txtTRAOrderID, new org.netbeans.lib.awtextra.AbsoluteConstraints(113, 66, 117, -1));

        jLabel3.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel3.setText("Customer:");
        add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 109, -1, -1));

        jLabel4.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel4.setText("Status:");
        add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 110, -1, -1));

        cbxTRAStatus.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15)); // NOI18N
        cbxTRAStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pending", "Delivered" }));
        cbxTRAStatus.setEnabled(false);
        add(cbxTRAStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(610, 110, -1, -1));

        jLabel5.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel5.setText("Date:");
        add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(522, 69, -1, -1));

        jLabel6.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel6.setText("Brand:");
        add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 150, -1, -1));

        cbxTRABrand.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        cbxTRABrand.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Carrier" }));
        add(cbxTRABrand, new org.netbeans.lib.awtextra.AbsoluteConstraints(610, 150, 233, -1));

        jLabel8.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel8.setText("List of Air-Conditioning Unit Models:");
        add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 275, -1, -1));

        listTRAUnits.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        listTRAUnits.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "FP-42CVSR016", "WCARP24EA", "WCARG010EC DLX" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(listTRAUnits);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 312, 242, 177));

        jLabel10.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel10.setText("Add to -------------->");
        add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 312, -1, -1));

        btnTRAAdd.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTRAAdd.setText("Add");
        btnTRAAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addModelActionPerformed(evt);
            }
        });
        add(btnTRAAdd, new org.netbeans.lib.awtextra.AbsoluteConstraints(298, 349, 96, -1));

        jLabel9.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel9.setText("Remove from ----->");
        add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 425, -1, -1));

        btnTRARemove.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTRARemove.setText("Remove");
        btnTRARemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeModelActionPerformed(evt);
            }
        });
        add(btnTRARemove, new org.netbeans.lib.awtextra.AbsoluteConstraints(298, 462, -1, -1));

        jLabel11.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel11.setText("Customer Orders:");
        add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(441, 275, -1, -1));

        tableTRACustomerOrders.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Serial Number", "Brand", "Model", "Quantity", "Price"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                true, false, false, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableTRACustomerOrders.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(tableTRACustomerOrders);
        tableTRACustomerOrders.getColumnModel().getColumn(0).setPreferredWidth(120);
        tableTRACustomerOrders.getColumnModel().getColumn(1).setPreferredWidth(100);
        tableTRACustomerOrders.getColumnModel().getColumn(2).setPreferredWidth(95);
        tableTRACustomerOrders.getColumnModel().getColumn(3).setPreferredWidth(10);
        tableTRACustomerOrders.getColumnModel().getColumn(4).setPreferredWidth(50);

        add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(441, 312, 694, 177));

        btnTRASave.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTRASave.setText("Save");
        btnTRASave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });
        add(btnTRASave, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 500, 96, -1));

        btnTRACancel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTRACancel.setText("Cancel");
        btnTRACancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });
        add(btnTRACancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(589, 500, 96, -1));

        listTRACustomers.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        listTRACustomers.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Abenson Inc. - Glorietta 5", "Ace Water Spa", "Rolex Plastic Manufacturing Corp.", "Star Paper Corp.", "Gerry's Grill - Trinoma" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listTRACustomers.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane3.setViewportView(listTRACustomers);

        add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(113, 109, 390, 148));
    }// </editor-fold>//GEN-END:initComponents
private void myInitComponents(){
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        txtTRAOrderID = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        cbxTRAStatus = new javax.swing.JComboBox();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        cbxTRABrand = new javax.swing.JComboBox();
        jLabel8 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        listTRAUnits = new javax.swing.JList();
        jLabel10 = new javax.swing.JLabel();
        btnTRAAdd = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        btnTRARemove = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        jScrollPane2 = new javax.swing.JScrollPane();
        tableTRACustomerOrders = new javax.swing.JTable();
        btnTRASave = new javax.swing.JButton();
        btnTRACancel = new javax.swing.JButton();
        jScrollPane3 = new javax.swing.JScrollPane();
        listTRACustomers = new javax.swing.JList();

        setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 25));
        jLabel1.setText("Add Customer Transaction");
        add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(376, 18, -1, -1));

        jLabel2.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel2.setText("Order ID:");
        add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 72, -1, -1));

        txtTRAOrderID.setEditable(false);
        txtTRAOrderID.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        add(txtTRAOrderID, new org.netbeans.lib.awtextra.AbsoluteConstraints(113, 66, 117, -1));

        jLabel3.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel3.setText("Customer:");
        add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 109, -1, -1));

        jLabel4.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel4.setText("Status:");
        add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 110, -1, -1));

        cbxTRAStatus.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        cbxTRAStatus.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Pending", "Delivered" }));
        add(cbxTRAStatus, new org.netbeans.lib.awtextra.AbsoluteConstraints(610, 110, -1, -1));

        jLabel5.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel5.setText("Date:");
        add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(522, 69, -1, -1));

        jLabel6.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel6.setText("Brand:");
        add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(520, 150, -1, -1));

        cbxTRABrand.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        cbxTRABrand.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Carrier" }));
        add(cbxTRABrand, new org.netbeans.lib.awtextra.AbsoluteConstraints(610, 150, 233, -1));

        jLabel8.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel8.setText("List of Air-Conditioning Unit Models:");
        add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 275, -1, -1));

        listTRAUnits.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        listTRAUnits.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "FP-42CVSR016", "WCARP24EA", "WCARG010EC DLX" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        jScrollPane1.setViewportView(listTRAUnits);

        add(jScrollPane1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 312, 242, 177));

        jLabel10.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel10.setText("Add to -------------->");
        add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 312, -1, -1));

        btnTRAAdd.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTRAAdd.setText("Add");
        btnTRAAdd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addModelActionPerformed(evt);
            }
        });
        add(btnTRAAdd, new org.netbeans.lib.awtextra.AbsoluteConstraints(298, 349, 96, -1));

        jLabel9.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel9.setText("Remove from ----->");
        add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(270, 425, -1, -1));

        btnTRARemove.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTRARemove.setText("Remove");
        btnTRARemove.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                removeModelActionPerformed(evt);
            }
        });
        add(btnTRARemove, new org.netbeans.lib.awtextra.AbsoluteConstraints(298, 462, -1, -1));

        jLabel11.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        jLabel11.setText("Customer Orders:");
        add(jLabel11, new org.netbeans.lib.awtextra.AbsoluteConstraints(441, 275, -1, -1));

        tableTRACustomerOrders.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null},
                {null, null, null, null, null}
            },
            new String [] {
                "Serial Number", "Brand", "Model", "Quantity", "Price"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class, java.lang.Integer.class, java.lang.Float.class
            };
            boolean[] canEdit = new boolean [] {
                true, false, false, true, true
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        tableTRACustomerOrders.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane2.setViewportView(tableTRACustomerOrders);
        tableTRACustomerOrders.getColumnModel().getColumn(0).setResizable(false);
        tableTRACustomerOrders.getColumnModel().getColumn(0).setPreferredWidth(120);
        tableTRACustomerOrders.getColumnModel().getColumn(1).setResizable(false);
        tableTRACustomerOrders.getColumnModel().getColumn(1).setPreferredWidth(100);
        tableTRACustomerOrders.getColumnModel().getColumn(2).setResizable(false);
        tableTRACustomerOrders.getColumnModel().getColumn(2).setPreferredWidth(95);
        tableTRACustomerOrders.getColumnModel().getColumn(3).setResizable(false);
        tableTRACustomerOrders.getColumnModel().getColumn(3).setPreferredWidth(10);
        tableTRACustomerOrders.getColumnModel().getColumn(4).setResizable(false);
        tableTRACustomerOrders.getColumnModel().getColumn(4).setPreferredWidth(50);

        add(jScrollPane2, new org.netbeans.lib.awtextra.AbsoluteConstraints(441, 312, 694, 177));

        btnTRASave.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTRASave.setText("Save");
        btnTRASave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                saveActionPerformed(evt);
            }
        });
        add(btnTRASave, new org.netbeans.lib.awtextra.AbsoluteConstraints(340, 500, 96, -1));

        btnTRACancel.setFont(new java.awt.Font("Arial Rounded MT Bold", 1, 15));
        btnTRACancel.setText("Cancel");
        btnTRACancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelActionPerformed(evt);
            }
        });
        add(btnTRACancel, new org.netbeans.lib.awtextra.AbsoluteConstraints(589, 500, 96, -1));

        listTRACustomers.setFont(new java.awt.Font("Arial Rounded MT Bold", 0, 15));
        listTRACustomers.setModel(new javax.swing.AbstractListModel() {
            String[] strings = { "Abenson Inc. - Glorietta 5", "Ace Water Spa", "Rolex Plastic Manufacturing Corp.", "Star Paper Corp.", "Gerry's Grill - Trinoma" };
            public int getSize() { return strings.length; }
            public Object getElementAt(int i) { return strings[i]; }
        });
        listTRACustomers.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane3.setViewportView(listTRACustomers);

        add(jScrollPane3, new org.netbeans.lib.awtextra.AbsoluteConstraints(113, 109, 390, 148));

        datePicker.setShowNumberOfWeek(true);
        datePicker.setShowNoneButton(false);
        datePicker.setFieldEditable(false);
        add(datePicker, new org.netbeans.lib.awtextra.AbsoluteConstraints(610, 70, -1, -1));
}
    private void initializeInstallationList(){
        installationQuery=new InstallationQuery(conn);
        installations=installationQuery.initializeInstallations();
        cbxTRABrand.removeAllItems();
        ArrayList<String> temp=new ArrayList<String>();
        for(int i=0;i<installations.size();i++)
            if(!temp.contains(installations.get(i).getBrand())){
                cbxTRABrand.addItem(installations.get(i).getBrand());
                temp.add(installations.get(i).getBrand());
            } //assures no duplicate brands will appear in dropdown list
        installations=installationQuery.search(installations.get(0).getBrand());
        cbxTRABrand.addActionListener(new ActionListener(){
            @Override
            public void actionPerformed(ActionEvent actionEvent){
                try{
                    installations=installationQuery.search(cbxTRABrand.getSelectedItem().toString());
                }catch(Exception e){}
                DefaultListModel listModel = new DefaultListModel();
                try{
                    for(int i=0;i<installations.size();i++)
                        listModel.addElement(installations.get(i).getModel());
                    listTRAUnits.setModel(listModel);
                }catch(Exception e){
                    installations=installationQuery.initializeInstallations();
                    for(int i=0;i<installations.size();i++)
                        listModel.addElement(installations.get(i).getModel());
                    listTRAUnits.setModel(listModel);
                }
            }
        });
        DefaultListModel listModel = new DefaultListModel();
        for(int i=0;i<installations.size();i++)
            listModel.addElement(installations.get(i).getModel());
        listTRAUnits.setModel(listModel);
    } //end of initializeInstallationList() method
    private void initializeCustomerList(){
        CustomerQuery customerQuery=new CustomerQuery(conn);
        customers=customerQuery.initializeCustomers();
        DefaultListModel listModel = new DefaultListModel();
        for(int i=0;i<customers.size();i++)
            listModel.addElement(customers.get(i).getName());
        listTRACustomers.setModel(listModel);
        listTRACustomers.setSelectedIndex(0);
    } //end of initializeCustomerList() method
    private boolean validTable(){
        for(int i=0;i<model.getRowCount();i++)
            for(int j=0;j<model.getColumnCount();j++)
                try{
                    if(model.getValueAt(i,j).equals(null))
                        return false;
                }catch(NullPointerException e){
                    return false;
                }
        return true;
    } //end of validTable() method
    private boolean tableIsEmpty(){
        for(int i=0;i<model.getRowCount();i++)
            for(int j=0;j<model.getColumnCount();j++)
                try{
                    if(!model.getValueAt(i,j).toString().isEmpty())
                        return false;
                }catch(NullPointerException e){
                    return false;
                }
        return true;
    } //end of tableIsEmpty() method
    private void getTableValues(){
        serialNumbers=new ArrayList<String>();
        brands=new ArrayList<String>();
        models=new ArrayList<String>();
        quantities=new ArrayList<Integer>();
        prices=new ArrayList<Float>();
        String[] rowData=new String[model.getColumnCount()];
        for(int i=0;i<model.getRowCount();i++){
            for(int j=0;j<model.getColumnCount();j++)
                rowData[j]=model.getValueAt(i, j).toString();
            serialNumbers.add(rowData[0]);
            brands.add(rowData[1]);
            models.add(rowData[2]);
            quantities.add(Integer.parseInt(rowData[3]));
            prices.add(Float.parseFloat(rowData[4]));
        }
    }
    private int getDayDiff(int year1, int month1, int day1, int year2, int month2, int day2){
        int dayDiff;
        int monthDiff;
        int yearDiff;
        if(day1<day2){
            day1+=30;
            month1-=1;
        }
        dayDiff=day1-day2;
        if(month1<month2){
            month1+=12;
            year1-=1;
        }
        monthDiff=month1-month2;
        yearDiff=year1-year2;
        dayDiff=yearDiff*12*30+monthDiff*30+dayDiff;
        return dayDiff;
    }
private void addModelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addModelActionPerformed
        Object[] rowData=new Object[4];
        rowData[1]=cbxTRABrand.getSelectedItem();
        rowData[2]=listTRAUnits.getSelectedValue();
        try{
            rowData[2].toString().equals(null);
        }
        catch(Exception e){
            rowData[2]=installations.get(0).getModel();
        }
        model.insertRow(nRows,rowData);
        nRows++;
        tableTRACustomerOrders.setModel(model);
        try{
            installationIDs.add(installations.get(listTRAUnits.getSelectedIndex()).getInstallationID());
        }
        catch(Exception e){
            installationIDs.add(installations.get(0).getInstallationID());
        }
}//GEN-LAST:event_addModelActionPerformed

private void removeModelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_removeModelActionPerformed
        if(tableTRACustomerOrders.isEditing())
            JOptionPane.showMessageDialog(null, "Can't remove while editing a cell!");
        else if(tableTRACustomerOrders.getSelectedRow() == -1)
            JOptionPane.showMessageDialog(null, "Nothing selected!");
        else{
            installationIDs.remove(tableTRACustomerOrders.getSelectedRow());
            model.removeRow(tableTRACustomerOrders.getSelectedRow());
            nRows--;
        }
        tableTRACustomerOrders.setModel(model);
}//GEN-LAST:event_removeModelActionPerformed

private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
        releaseQuery=new TransactionReleaseQuery(conn);
        if(tableTRACustomerOrders.isEditing())
            JOptionPane.showMessageDialog(null, "Can't save while editing a cell!");
        else if(!validTable() || tableIsEmpty())
            JOptionPane.showMessageDialog(null, "All fields cannot be empty!");
        else if(datePicker.getDate().after(date))
            JOptionPane.showMessageDialog(null, "Stay on the current time!");
        else if(getDayDiff(date.getYear(),date.getMonth(),date.getDate(),datePicker.getDate().getYear(),datePicker.getDate().getMonth(),datePicker.getDate().getDate())>180)
            JOptionPane.showMessageDialog(null, "Stay on the current time!");
        else{
            JOptionPane pane=new JOptionPane("Are you sure you want to add this release?");
            Object[] options=new String[]{"Yes","No"};
            pane.setOptions(options);
            JDialog dialog = pane.createDialog(new JFrame(), "Confirm Release Addition");
            dialog.show();
            if(options[0].equals(pane.getValue())){
                getTableValues();
                releaseQuery.addRelease(releaseQuery.setID(),customers.get(listTRACustomers.getSelectedIndex()).getID(),customers.get(listTRACustomers.getSelectedIndex()).getName(),datePicker.getDate(),cbxTRAStatus.getSelectedItem().toString(),serialNumbers,quantities,prices,installationIDs,brands,models);
                backToMainMenu=true;
            } //YES
        }
}//GEN-LAST:event_saveActionPerformed

private void cancelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cancelActionPerformed
        if(tableIsEmpty())
            backToMainMenu=true;
        else{
            JOptionPane pane=new JOptionPane("Are you sure you want to go back to the main menu? Please make sure that all changes have been saved.");
            Object[] options=new String[]{"Yes","No"};
            pane.setOptions(options);
            JDialog dialog = pane.createDialog(new JFrame(), "Back To Main Menu?");
            dialog.show();
            if(options[0].equals(pane.getValue())) //YES
                backToMainMenu=true;
        }
}//GEN-LAST:event_cancelActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnTRAAdd;
    private javax.swing.JButton btnTRACancel;
    private javax.swing.JButton btnTRARemove;
    private javax.swing.JButton btnTRASave;
    private javax.swing.JComboBox cbxTRABrand;
    private javax.swing.JComboBox cbxTRAStatus;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JList listTRACustomers;
    private javax.swing.JList listTRAUnits;
    private javax.swing.JTable tableTRACustomerOrders;
    private javax.swing.JTextField txtTRAOrderID;
    // End of variables declaration//GEN-END:variables
}
